package source ;
import sys.db.Connection;
import sys.db.SqLite;
import flixel.FlxState;
import flixel.text.FlxText;
import flixel.FlxG;
import sys.db.ResultSet;
/**
 * ...
 * @author ...
 */
class DBHelper
{
	public static var cnx: Connection;

	public function new()
	{
		var cnx = sys.db.Sqlite.open("assets/TestDB.sqlite");
		//var rset = GetAnswerByQuestionId(1);
				
		//for ( row in rset ) {
		//	trace(row.Answer);
		//}
		//Para recorrer el result set (row.Anwser)
		//for( row in rset ) {
		//	var text = new FlxText(FlxG.width / 2, FlxG.height / 2, FlxG.width / 2, row.Answer,25);
		//	add(text);
		//}
	}
	
	
	public static function GetQuestionTable():ResultSet
	{
		var cnx = OpenConnection();
		var rset = cnx.request("SELECT * FROM Questions");
		CloseConnection(cnx);
		return rset;
	}
	
	public static function GetQuestionById(questionId: Int):ResultSet
	{
		var cnx = OpenConnection();
		var rset = cnx.request("SELECT * FROM Questions where QuestionId = " + questionId);
		CloseConnection(cnx);
		return rset;
	}
	
	public static function GetAnswerByQuestionId(questionId: Int):ResultSet
	{
		var cnx = OpenConnection();
		var rset = cnx.request("SELECT * FROM Answers where QuestionId = " + questionId);
		CloseConnection(cnx);
		return rset;
	}
	
	public static function OpenConnection(): Connection
	{
		return sys.db.Sqlite.open("assets/TestDB.sqlite");
	}
	
	public static function CloseConnection(cnxToClose: Connection)
	{
		cnxToClose.close();
	}
		
}